package com.fr.chart.core.glyph;

import com.fr.base.FRFont;
import com.fr.base.Inter;
import com.fr.base.core.GraphHelper;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.chart.axis.Axis;
import com.fr.chart.axis.TextAttr;
import com.fr.chart.axis.ValueAxis;
import com.fr.chart.base.equals.Equals;
import com.fr.report.cellElement.Formula;
import com.fr.report.script.core.FArray;
import com.fr.util.Utils;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.Stroke;
import java.awt.geom.Dimension2D;
import java.awt.geom.Line2D;
import java.awt.geom.Line2D.Double;
import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Double;
import java.awt.geom.Rectangle2D.Double;
import java.awt.geom.RectangularShape;
import java.math.BigDecimal;
import java.text.Format;

public class ValueAxisGlyph extends AxisGlyph
{
  private static final long serialVersionUID = 2649354991884111487L;
  public static final String XML_TAG = "ValueAxisGlyph";
  protected ValueAxis valueAxis;

  public ValueAxisGlyph()
  {
  }

  public ValueAxisGlyph(Axis paramAxis)
  {
    super(paramAxis);
  }

  public void calculateAxisLengthUnit()
  {
    if (this.valueAxis.isLog())
      this.unitLength = (this.axisLength / (Math.log(this.maxValue) / Math.log(this.tickLength) - Math.log(this.minValue) / Math.log(this.tickLength)));
    else if (this.maxValue - this.minValue > 0.0D)
      this.unitLength = (this.axisLength / (this.maxValue - this.minValue));
  }

  public void axisExtends(int paramInt1, int paramInt2)
  {
    setMaxValue(getMaxValue() + paramInt1);
    setMinValue(getMinValue() - paramInt2);
    calculateAxisLengthUnit();
  }

  public void initMinMaxValue(double paramDouble1, double paramDouble2)
  {
    if (this.valueAxis.isCustomMinValue())
    {
      this.minValue = this.valueAxis.getMinValue();
      paramDouble1 = this.minValue;
    }
    else
    {
      this.minValue = paramDouble1;
    }
    if (this.valueAxis.isCustomMaxValue())
    {
      this.maxValue = this.valueAxis.getMaxValue();
      paramDouble2 = this.maxValue;
    }
    if (this.valueAxis.isLog())
    {
      if (this.valueAxis.isCustomIncrement())
        this.tickLength = ((ValueAxis)getAxis()).getIncrement();
      else
        this.tickLength = 10.0D;
      this.smallTickLength = (this.tickLength / 5.0D);
    }
    else if (paramDouble2 - paramDouble1 > 0.0D)
    {
      if (this.valueAxis.isCustomIncrement())
      {
        this.tickLength = ((ValueAxis)getAxis()).getIncrement();
        if (this.valueAxis.isPercentage())
          this.tickLength /= 100.0D;
      }
      else
      {
        this.tickLength = Axis.calculateIncrement(this.minValue, paramDouble2);
      }
      this.smallTickLength = (this.tickLength / 5.0D);
    }
    if ((paramDouble2 > paramDouble1) && ((paramDouble2 - paramDouble1) % this.tickLength != 0.0D))
      paramDouble2 = paramDouble2 + this.tickLength - (paramDouble2 - paramDouble1) % this.tickLength;
    if (!(this.valueAxis.isCustomMaxValue()))
      this.maxValue = (paramDouble2 + this.tickLength);
    if ((!(this.valueAxis.isCustomMinValue())) && (this.minValue < 0.0D))
      this.minValue -= this.tickLength;
    if (this.minValue >= this.maxValue)
      this.maxValue = (this.minValue + 10.0D);
    if (this.valueAxis.isLog())
      this.minValue = Math.max(1.0D, this.minValue);
  }

  public Axis getAxis()
  {
    return this.valueAxis;
  }

  public void setAxis(Axis paramAxis)
  {
    if (!(paramAxis instanceof ValueAxis))
      throw new IllegalArgumentException();
    this.valueAxis = ((ValueAxis)paramAxis);
  }

  public double getCrossValue()
  {
    if (this.valueAxis.isCustomCrossValue())
      return this.valueAxis.getCrossValue();
    return this.minValue;
  }

  public String value2String(double paramDouble)
  {
    Format localFormat = getAxis().getFormat();
    String str = null;
    if (localFormat != null)
      str = localFormat.format(Double.valueOf(Utils.doubleToString(paramDouble)));
    else
      str = String.valueOf(paramDouble);
    return str;
  }

  public Point2D getPoint2D(double paramDouble)
  {
    if ((this.valueAxis.isLog()) && (getCrossValue() > 0.0D))
    {
      if (paramDouble <= 1.0E-0D)
        paramDouble = getCrossValue();
      paramDouble = (Math.log(paramDouble) - Math.log(getCrossValue())) / Math.log(this.tickLength);
    }
    else
    {
      paramDouble -= getCrossValue();
    }
    if ((getAxis().getPosition() == 2) || (getAxis().getPosition() == 4))
      return new Point2D.Double(this.originPoint.getX(), this.originPoint.getY() - this.unitLength * paramDouble);
    return new Point2D.Double(this.originPoint.getX() + this.unitLength * paramDouble, this.originPoint.getY());
  }

  protected void drawAxisGrid(Graphics paramGraphics)
  {
    Object localObject1;
    Object localObject2;
    double d1;
    Object localObject4;
    int j;
    Line2D[] arrayOfLine2D3;
    int l;
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    if ((getAxis().getSecondGridStyle() != 0) && (this.smallTickLength > 0.0D))
    {
      localObject1 = localGraphics2D.getStroke();
      localObject2 = localGraphics2D.getPaint();
      localGraphics2D.setStroke(GraphHelper.getStroke(getAxis().getSecondGridStyle()));
      localGraphics2D.setPaint(getAxis().getSecondGridColor());
      if (this.valueAxis.isLog())
      {
        if (this.smallTickLength <= 1.0D)
          break label256;
        d1 = getCrossValue();
        double d3 = Math.log(d1) / Math.log(this.smallTickLength);
        while (true)
        {
          if (d1 > this.maxValue)
            break label256;
          if ((d1 - getCrossValue()) / this.smallTickLength == 5.0D)
            break label166:
          arrayOfLine2D3 = getGridLine(d1);
          for (l = 0; l < arrayOfLine2D3.length; ++l)
            localGraphics2D.draw(arrayOfLine2D3[l]);
          label166: d1 = Math.exp(++d3 * Math.log(this.smallTickLength));
        }
      }
      d1 = this.minValue + this.smallTickLength;
      while (d1 <= this.maxValue)
      {
        Line2D[] arrayOfLine2D1 = getGridLine(d1);
        for (j = 0; j < arrayOfLine2D1.length; ++j)
          localGraphics2D.draw(arrayOfLine2D1[j]);
        d1 += this.smallTickLength;
      }
      label256: localGraphics2D.setPaint((Paint)localObject2);
      localGraphics2D.setStroke((Stroke)localObject1);
    }
    if ((getAxis().getMainGridStyle() != 0) && (this.tickLength > 0.0D))
    {
      localObject1 = localGraphics2D.getStroke();
      localObject2 = localGraphics2D.getPaint();
      localGraphics2D.setStroke(GraphHelper.getStroke(getAxis().getMainGridStyle()));
      localGraphics2D.setPaint(getAxis().getMainGridColor());
      if (this.valueAxis.isLog())
      {
        if (this.tickLength <= 1.0D)
          break label496;
        d1 = getCrossValue();
        double d4 = Math.log(d1) / Math.log(this.tickLength);
        while (true)
        {
          if (d1 > this.maxValue)
            break label496;
          arrayOfLine2D3 = getGridLine(d1);
          for (l = 0; l < arrayOfLine2D3.length; ++l)
            localGraphics2D.draw(arrayOfLine2D3[l]);
          d1 = Math.exp(++d4 * Math.log(this.tickLength));
        }
      }
      d1 = this.minValue + this.tickLength;
      while (d1 <= this.maxValue)
      {
        localObject4 = getGridLine(d1);
        for (j = 0; j < localObject4.length; ++j)
          localGraphics2D.draw(localObject4[j]);
        d1 += this.tickLength;
      }
      label496: localGraphics2D.setPaint((Paint)localObject2);
      localGraphics2D.setStroke((Stroke)localObject1);
    }
    if (this.valueAxis.hasAlertValue())
    {
      Line2D[] arrayOfLine2D2;
      int k;
      localObject1 = localGraphics2D.getPaint();
      localGraphics2D.setPaint(Color.RED);
      localObject2 = this.valueAxis.getAlertValue();
      if (localObject2 != null)
      {
        int i;
        if (localObject2 instanceof Formula)
        {
          localObject3 = getGridLine((this.maxValue - this.minValue) / 4.0D);
          i = 0;
          while (true)
          {
            if (i >= localObject3.length)
              break label790;
            localGraphics2D.draw(localObject3[i]);
            ++i;
          }
        }
        if (localObject2 instanceof FArray)
        {
          localObject3 = (FArray)localObject2;
          i = 0;
          while (true)
          {
            Line2D[] arrayOfLine2D4;
            int i1;
            if (i >= ((FArray)localObject3).length())
              break label790;
            localObject4 = Utils.string2Number(((FArray)localObject3).elementAt(i).toString());
            if (localObject4 != null)
            {
              double d5 = ((Number)localObject4).doubleValue();
              if ((d5 <= getMaxValue()) && (d5 >= getMinValue()))
              {
                arrayOfLine2D4 = getGridLine(d5);
                for (i1 = 0; i1 < arrayOfLine2D4.length; ++i1)
                  localGraphics2D.draw(arrayOfLine2D4[i1]);
              }
            }
            ++i;
          }
        }
        Object localObject3 = localObject2.toString();
        if (Utils.string2Number((String)localObject3) != null)
        {
          double d2 = Utils.string2Number((String)localObject3).doubleValue();
          if ((d2 <= getMaxValue()) && (d2 >= getMinValue()))
          {
            arrayOfLine2D2 = getGridLine(d2);
            for (k = 0; k < arrayOfLine2D2.length; ++k)
              localGraphics2D.draw(arrayOfLine2D2[k]);
          }
        }
      }
      label790: localGraphics2D.setPaint((Paint)localObject1);
    }
  }

  protected void drawAxisLine(Graphics paramGraphics)
  {
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    if (this.minValue == this.maxValue)
      return;
    Paint localPaint = localGraphics2D.getPaint();
    Color localColor = getAxis().getLineColor();
    int i = getAxis().getLineStyle();
    if ((localColor != null) && (i != 0) && (getAxis().isAxisShow()))
    {
      localGraphics2D.setPaint(localColor);
      Line2D.Double localDouble = new Line2D.Double(getPoint2DForSelf(this.minValue), getPoint2DForSelf(this.maxValue));
      GraphHelper.draw(localGraphics2D, localDouble, i);
      drawArrow(localGraphics2D, getPoint2DForSelf(this.minValue), getPoint2DForSelf(this.maxValue));
    }
    drawTicks(localGraphics2D);
    localGraphics2D.setPaint(localPaint);
  }

  public void drawTicks(Graphics paramGraphics)
  {
    Object localObject2;
    Object localObject3;
    double d3;
    Object localObject5;
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    BasicStroke localBasicStroke1 = new BasicStroke(this.valueAxis.getMainStroke(), 1, 0);
    BasicStroke localBasicStroke2 = new BasicStroke(this.valueAxis.getSecStroke(), 1, 0);
    if (getAxis().isAxisShow())
    {
      if (this.valueAxis.isLog())
      {
        if (this.tickLength > 1.0D)
        {
          d1 = getCrossValue();
          d2 = Math.log(d1) / Math.log(this.tickLength);
          while (d1 <= this.maxValue)
          {
            localObject2 = getTickLine(d1, this.valueAxis.getTickLength(), this.valueAxis.getTickMarkType());
            if (this.valueAxis.getTickLength() != 0)
            {
              if (this.valueAxis.getMainStroke() == 0.0F)
                break label155:
              drawTickLine(paramGraphics, (Line2D)localObject2, localBasicStroke1);
            }
            label155: d1 = Math.exp(++d2 * Math.log(this.tickLength));
          }
        }
        if (this.smallTickLength <= 1.0D)
          break label498;
        d1 = getCrossValue();
        double d2 = Math.log(d1) / Math.log(this.smallTickLength);
        while (true)
        {
          if (d1 > this.maxValue)
            break label498;
          if ((d1 - getCrossValue()) / this.smallTickLength == 5.0D)
            break label296:
          localObject2 = getTickLine(d1, this.valueAxis.getSecTickLength(), this.valueAxis.getSecTickMarkType());
          if (this.valueAxis.getSecTickLength() != 0)
          {
            if (this.valueAxis.getSecStroke() == 0.0F)
              break label296:
            drawTickLine(paramGraphics, (Line2D)localObject2, localBasicStroke2);
          }
          label296: d1 = Math.exp(++d2 * Math.log(this.smallTickLength));
        }
      }
      if ((this.maxValue > this.minValue) && (this.tickLength > 0.0D) && (this.smallTickLength > 0.0D))
      {
        d1 = this.minValue + this.tickLength;
        while (d1 <= this.maxValue)
        {
          localObject1 = getTickLine(d1, this.valueAxis.getTickLength(), this.valueAxis.getTickMarkType());
          drawTickLine(paramGraphics, (Line2D)localObject1, localBasicStroke1);
          d1 += this.tickLength;
        }
        d1 = this.minValue + this.smallTickLength;
        while (d1 <= this.maxValue)
        {
          if ((d1 - this.minValue) / this.smallTickLength == 5.0D)
            break label486:
          localObject1 = getTickLine(d1, this.valueAxis.getSecTickLength(), this.valueAxis.getSecTickMarkType());
          drawTickLine(paramGraphics, (Line2D)localObject1, localBasicStroke2);
          label486: d1 += this.smallTickLength;
        }
      }
    }
    label498: double d1 = 1.0D;
    Object localObject1 = this.valueAxis.getShowUnit();
    if (localObject1 != null)
    {
      d1 = ValueAxis.getDivideUnit((String)localObject1);
      TextGlyph localTextGlyph = new TextGlyph(Inter.getLocText("Unit") + ":" + ((String)localObject1), null);
      localObject2 = null;
      if (getAxis().getPosition() == 1)
        localObject2 = new Rectangle(0, -30, 100, 20);
      else if (getAxis().getPosition() == 2)
        localObject2 = new Rectangle(-20, -25, 100, 20);
      else if (getAxis().getPosition() == 3)
        localObject2 = new Rectangle(0, (int)getAxisLabelWidth(), 100, 20);
      else if (getAxis().getPosition() == 4)
        localObject2 = new Rectangle(15, -25, 100, 20);
      localTextGlyph.setBounds((RectangularShape)localObject2);
      localTextGlyph.draw(localGraphics2D);
    }
    int i = 0;
    int j = 0;
    if ((getAxis().getLabelNumber() == -1) || (getAxis().getLabelNumber() == 0))
      j = 1;
    else
      j = getAxis().getLabelNumber();
    BigDecimal localBigDecimal1 = new BigDecimal(Double.toString(this.tickLength));
    BigDecimal localBigDecimal2 = new BigDecimal(Double.toString(j));
    if (this.valueAxis.isLog())
    {
      if (this.tickLength <= 1.0D)
        break label1090;
      localObject3 = new BigDecimal(Double.toString(getCrossValue()));
      d3 = getCrossValue();
      while (true)
      {
        if (d3 > this.maxValue)
          break label1090;
        localObject5 = ((BigDecimal)localObject3).multiply(new BigDecimal(Double.toString(1.0D / d1)));
        if (getAxis().isStagger())
          drawBGLabel(paramGraphics, d3, 0.0D, value2String(((BigDecimal)localObject5).doubleValue()), i);
        else
          drawLabel(paramGraphics, d3, 0.0D, value2String(((BigDecimal)localObject5).doubleValue()));
        ++i;
        localObject3 = ((BigDecimal)localObject3).multiply(localBigDecimal1.multiply(localBigDecimal2));
        d3 = d3 * this.tickLength * j;
      }
    }
    if ((this.maxValue > this.minValue) && (this.tickLength > 0.0D) && (this.smallTickLength > 0.0D))
    {
      localObject3 = new BigDecimal(Double.toString(this.minValue));
      d3 = this.minValue;
      while (d3 <= this.maxValue)
      {
        localObject5 = ((BigDecimal)localObject3).multiply(new BigDecimal(Double.toString(1.0D / d1)));
        if (getAxis().isStagger())
          drawBGLabel(paramGraphics, d3, 0.0D, value2String(((BigDecimal)localObject5).doubleValue()), i);
        else
          drawLabel(paramGraphics, d3, 0.0D, value2String(((BigDecimal)localObject5).doubleValue()));
        ++i;
        localObject3 = ((BigDecimal)localObject3).add(localBigDecimal1.multiply(localBigDecimal2));
        d3 += this.tickLength * j;
      }
    }
    if (this.valueAxis.hasAlertValue())
    {
      label1090: localObject3 = this.valueAxis.getAlertValue();
      if (localObject3 != null)
        if (localObject3 instanceof Formula)
        {
          drawAlertValueLabel(paramGraphics, (this.maxValue - this.minValue) / 4.0D, 0.0D, Inter.getLocText("ChartF-Alert"));
        }
        else
        {
          if (localObject3 instanceof FArray)
          {
            localObject4 = (FArray)localObject3;
            int k = 0;
            while (true)
            {
              if (k >= ((FArray)localObject4).length())
                return;
              localObject5 = Utils.string2Number(((FArray)localObject4).elementAt(k).toString());
              if (localObject5 != null)
              {
                double d5 = ((Number)localObject5).doubleValue();
                if ((d5 >= getMinValue()) && (d5 <= getMaxValue()))
                  drawAlertValueLabel(paramGraphics, d5, 0.0D, ((FArray)localObject4).elementAt(k).toString());
              }
              ++k;
            }
          }
          Object localObject4 = Utils.string2Number(localObject3.toString());
          if (localObject4 != null)
          {
            double d4 = ((Number)localObject4).doubleValue();
            if ((d4 >= getMinValue()) && (d4 <= getMaxValue()))
              drawAlertValueLabel(paramGraphics, d4, 0.0D, localObject3.toString());
          }
        }
    }
  }

  private void drawAlertValueLabel(Graphics paramGraphics, double paramDouble1, double paramDouble2, String paramString)
  {
    Color localColor = paramGraphics.getColor();
    Point2D localPoint2D = getPoint2DForSelf(paramDouble1 + paramDouble2);
    TextAttr localTextAttr = new TextAttr();
    localTextAttr.setFRFont(FRFont.getInstance("SimSun", 12, 12, Color.red));
    Dimension2D localDimension2D = TextGlyph.calculateTextDimension(paramString, localTextAttr);
    Rectangle2D.Double localDouble = null;
    int i = getAxis().getPosition();
    if (i == 2)
      localDouble = new Rectangle2D.Double(localPoint2D.getX() + getAxisGridLength(), localPoint2D.getY() - localDimension2D.getHeight() / 2.0D, localDimension2D.getWidth(), localDimension2D.getHeight());
    else if (i == 1)
      localDouble = new Rectangle2D.Double(localPoint2D.getX() - localDimension2D.getWidth() / 2.0D, localPoint2D.getY() + getAxisGridLength() + localDimension2D.getHeight(), localDimension2D.getWidth(), localDimension2D.getHeight());
    else if (i == 4)
      localDouble = new Rectangle2D.Double(localPoint2D.getX() - localDimension2D.getWidth() - getAxisGridLength(), localPoint2D.getY() - localDimension2D.getHeight(), localDimension2D.getWidth(), localDimension2D.getHeight());
    else
      localDouble = new Rectangle2D.Double(localPoint2D.getX() - localDimension2D.getWidth() / 2.0D, localPoint2D.getY() - getAxisGridLength() - localDimension2D.getHeight() - 4.0D, localDimension2D.getWidth(), localDimension2D.getHeight());
    TextGlyph.drawLabel(paramGraphics, paramString, localTextAttr, localDouble);
    paramGraphics.setColor(localColor);
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof ValueAxisGlyph))
      return false;
    ValueAxisGlyph localValueAxisGlyph = (ValueAxisGlyph)paramObject;
    if (!(super.equals(localValueAxisGlyph)))
      return false;
    return (Equals.equals(localValueAxisGlyph.getAxis(), getAxis()));
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("ValueAxisGlyph");
    super.writeXML(paramXMLPrintWriter);
    if (this.valueAxis != null)
      this.valueAxis.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str2;
      String str1 = paramXMLableReader.getTagName();
      if (str1.equals("Attr"))
      {
        if ((str2 = paramXMLableReader.getAttr("minValue")) != null)
          setMinValue(Double.valueOf(str2).doubleValue());
        if ((str2 = paramXMLableReader.getAttr("maxValue")) != null)
          setMaxValue(Double.valueOf(str2).doubleValue());
      }
      else if (str1.equals("ValueAxis"))
      {
        if (((str2 = paramXMLableReader.getAttr("class")) != null) && (str2.endsWith(".ValueAxis")))
          this.valueAxis = ((ValueAxis)paramXMLableReader.readXMLObject(new ValueAxis()));
      }
    }
  }

  public JSONObject toJSONObject()
    throws JSONException
  {
    JSONObject localJSONObject = super.toJSONObject();
    if (this.valueAxis != null)
      localJSONObject.put("valueAxis", this.valueAxis.toJSONObject());
    return localJSONObject;
  }

  public String getJSAxisType()
  {
    return "ValueAxisGlyph";
  }
}